<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./css/style.css">
    <title>注册学生账号</title>
    
    <link rel="stylesheet" type="text/css" href="easyui/1.3.4/themes/default/easyui.css" />
    <script type="text/javascript" src="js/axios.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="easyui/1.3.4/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="easyui/1.3.4/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<h1>修改经费申请</h1>
<div class="info">
    <div class="form">
        <div class="left">
            <div class="form-item">
                <div class="label">项目id</div>
                ：
                <input type="text" id="pid">
            </div>
            <div class="form-item">
                <div class="label">申请数额</div>
                ：
                <input type="number" id="amount">
            </div>
            <div class="form-item">
                <div class="label">申请时间</div>
                ：
                <input type="date" id="date">
            </div>
            <div class="form-item">
                <div class="label" >申请状态</div>
                ：
                <input type="text" id="status">
            </div>
            <div class="form-item">
                <div class="label" >发起人</div>
                ：
                <input type="text" id="approvalsponsor">
            </div>
         
        </div>
    </div>
    <div class="link" style="align-self: start">
        <div class="link-item" id="send-request">修改</div>
        <div class="link-item" id="reset">重置</div>
    </div>

</div>
<script>
    const user = JSON.parse(localStorage.getItem("userinfo"));
    const status= $("#status");
    const pid = $("#pid");
    const amount = $("#amount");
    const date = $("#date");
    const approvalsponsor=$("#approvalsponsor");
    const sendRequest = $("#send-request");
    const reset = $("#reset");
    //记录是否为本人
    let identity=false;
    
    const id = location.search.split("=")[1];
    
    axios.get("http://localhost:8080/fundingApproval/detail?id=" + id).then(({data}) => {
        pid.val(data.data.projectId);
        amount.val(data.data.amount);
        date.val(data.data.approvalDate);
        approvalsponsor.val(data.data.approvalSponsor);
        status.val(data.data.approvalStatus);
        identity=(data.data.approvalSponsor==user.teacherName);
    })
    
    sendRequest.click(function () {
         //只有本人和管理员能修改信息
         if(user.teacherTitle!="用户"){
            axios.post("http://localhost:8080/fundingApproval/update", {
            approvalId: id,
            projectId: pid.val(),
            amount: Number.parseInt(amount.val()),
            approvalDate: date.val(),
            approvalStatus: status.val(),
            approvalSponsor: approvalsponsor.val(),
        }).then(({data}) => {
            if (data.data == null) {
                throw new Error(data.msg);
            }
            alert("修改成功！");
            top.location.reload(true);
        }).catch(err => {
            alert(err);
        })
         }
         else{
            alert("无权修改信息");
            top.location.reload(true);
         }
       
    })
    
    reset.click(function () {
        name.val("");
        address.val("");
        phone.val("");
        fax.val("");
    })

</script>
</body>
</html>